package src.bean;

import java.util.HashMap;
import java.util.Map;

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.render.ClientEvent;

import oracle.ui.pattern.dynamicShell.Tab;
import oracle.ui.pattern.dynamicShell.TabContext;

public class Launcher {
    public Launcher() {
        super();
    }

    private void _launchActivity(String title, String taskflowId, boolean newTab) {
        try {
            TabContext.getCurrentInstance().setTabsRendered(true);
            if (newTab) {
                TabContext.getCurrentInstance().addTab(title, taskflowId);
            } else {
                TabContext.getCurrentInstance().addOrSelectTab(title, taskflowId);
            }
        } catch (TabContext.TabOverflowException toe) {
            // causes a dialog to be displayed to the user saying that there are
            // too many tabs open - the new tab will not be opened...
            toe.printStackTrace();
        }
    }

    private void _launchActivity(String title, String taskflowId, boolean newTab, Map parameters) {
        try {
            TabContext.getCurrentInstance().setTabsRendered(true);
            if (newTab) {
                TabContext.getCurrentInstance().addTab(title, taskflowId, parameters);
            } else {
                TabContext.getCurrentInstance().addOrSelectTab(title, taskflowId, parameters);
            }
        } catch (TabContext.TabOverflowException toe) {
            // causes a dialog to be displayed to the user saying that there are
            // too many tabs open - the new tab will not be opened...
            toe.printStackTrace();
        }
    }

    public void launchDefaultEmployeeTab(ClientEvent clientEvent) {
        _launchActivity("Employee Tab", "/WEB-INF/employee-tf.xml#employee-tf", false);
    }

    public void launchDefaultDepartmentTab(ClientEvent clientEvent) {
        _launchActivity("Department Tab", "/WEB-INF/department-tf.xml#department-tf", false);
    }

    public void launchDefaultJobTab(ClientEvent clientEvent) {
        _launchActivity("Job Tab", "/WEB-INF/job-tf.xml#job-tf", false);
    }

    public void launchEmployeeDetailTab(ActionEvent actionEvent) {
        Object employeeId = actionEvent.getComponent().getAttributes().get("pEmployeeId");
        Object firstName = actionEvent.getComponent().getAttributes().get("pFirstName");
        Object lastName = actionEvent.getComponent().getAttributes().get("pLastName");
        String title = (String) firstName + " " + lastName + " Details";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("pEmployeeId", employeeId);
        _launchActivity(title, "/WEB-INF/emptDetail-tf.xml#emptDetail-tf", true, param);
    }

    public void closeTab() {
        TabContext tabContext = TabContext.getCurrentInstance();
        int tabIndex = tabContext.getSelectedTabIndex();
        if (tabIndex != -1) {
            tabContext.removeTab(tabIndex);
        }
    }

    public void updateCurrentTabTitle(String title) {
        TabContext tabContext = TabContext.getCurrentInstance();
        int currentTabIndex = tabContext.getSelectedTabIndex();
        Tab currentTab = tabContext.getTabs().get(currentTabIndex);
        currentTab.setTitle(title);
        //force refresh
        tabContext.setSelectedTabIndex(currentTabIndex);
    }
}
